package com.ruoyi.system.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.system.domain.MxWareIn;
import com.ruoyi.system.domain.MxWareLog;
import com.ruoyi.system.domain.MxWareOut;
import com.ruoyi.system.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class MxInOutService {


    @Autowired
    private MxWareInMapper mxWareInMapper;

    @Autowired
    private MxWareLogMapper mxWareLogMapper;

    @Autowired
    private MxWareMapper mxWareMapper;

    @Autowired
    private MxWareOutMapper mxWareOutMapper;

    @Autowired
    private DateUtilSqlMapper dateUtilSqlMapper;

    // 入库
    @Transactional
    public void insaveWare(MxWareIn mxWareIn){
        // 入库表
        mxWareIn.setAddtime(dateUtilSqlMapper.getNowDateTime());
        mxWareIn.setUpTime(dateUtilSqlMapper.getNowDateTime());
        mxWareInMapper.insertMxWareIn(mxWareIn);

        // 出入库日志表
        String obj = JSON.toJSONString(mxWareIn);
        MxWareLog mxWareLog = JSONObject.parseObject(obj, MxWareLog.class);
        String remark = mxWareLog.getRemark();
        mxWareLog.setRemark("入库："+remark);
        mxWareLogMapper.insertMxWareLog(mxWareLog);
        // 增加库存
        int num = Integer.parseInt(mxWareIn.getNum()+"");
        String wareId = mxWareIn.getWareId()+"";
        mxWareMapper.inWareAddNum(wareId,num);
    }


    // 出库
    @Transactional
    public void outSaveWare(MxWareOut mxWareOut){
        // 入库表
        mxWareOut.setAddtime(dateUtilSqlMapper.getNowDateTime());
        mxWareOut.setUpTime(dateUtilSqlMapper.getNowDateTime());
        mxWareOutMapper.insertMxWareOut(mxWareOut);

        // 出入库日志表
        String obj = JSON.toJSONString(mxWareOut);
        MxWareLog mxWareLog = JSONObject.parseObject(obj, MxWareLog.class);
        String remark = mxWareLog.getRemark();
        mxWareLog.setRemark("出库："+remark);
        mxWareLogMapper.insertMxWareLog(mxWareLog);
        // 增加库存
        int num = Integer.parseInt(mxWareOut.getNum()+"");
        String wareId = mxWareOut.getWareId()+"";
        mxWareMapper.inWareOutNum(wareId,num);
    }
}
